package cn.stylefeng.guns.core.schedule;

import cn.stylefeng.guns.sys.modular.system.model.params.BtyDingdingDayAttendParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyDingdingDayDeptParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyDingdingDayEmpParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyDingdingMonthAttendParam;
import cn.stylefeng.guns.sys.modular.system.service.BtyDingdingDayAttendService;
import cn.stylefeng.guns.sys.modular.system.service.BtyDingdingDayDeptService;
import cn.stylefeng.guns.sys.modular.system.service.BtyDingdingDayEmpService;
import cn.stylefeng.guns.sys.modular.system.service.BtyDingdingMonthAttendService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 *  钉钉考勤的定时任务，xxl-job
 *
 */
@Component
public class DingdingAttendTask {
    private Logger logger = LoggerFactory.getLogger(DingdingAttendTask.class);

    // 获取部门数据
    @Autowired
    private BtyDingdingDayDeptService btyDingdingDayDeptService;

    // 获取员工数据
    @Autowired
    private BtyDingdingDayEmpService btyDingdingDayEmpService;

    // 获取员工，月份每天的考勤数据
    @Autowired
    private BtyDingdingDayAttendService btyDingdingDayAttendService;

    // 获取员工月份的考勤数据
    @Autowired
    private BtyDingdingMonthAttendService btyDingdingMonthAttendService;

    /**
     * 同步月度考勤信息；
     *
     */
    @XxlJob("taskSynchronousAttend")
    public void taskSynchronousAttend() throws Exception {
        long t1=System.currentTimeMillis();

        Thread.sleep(1000);

        BtyDingdingMonthAttendParam btyDingdingMonthAttendParam = new BtyDingdingMonthAttendParam();
        BtyDingdingDayDeptParam btyDingdingDayDeptParam = new BtyDingdingDayDeptParam();
        btyDingdingDayDeptParam.setAttendYearMonth(btyDingdingMonthAttendParam.getAttendYearMonth());
        Map<String,String> map1 = this.btyDingdingDayDeptService.addBtyDingdingMonthDept(btyDingdingDayDeptParam);

        Thread.sleep(1000);

        BtyDingdingDayEmpParam btyDingdingDayEmpParam = new BtyDingdingDayEmpParam();
        btyDingdingDayEmpParam.setAttendYearMonth(btyDingdingMonthAttendParam.getAttendYearMonth());
        Map<String,String> map2 = this.btyDingdingDayEmpService.addBtyDingdingMonthEmp(btyDingdingDayEmpParam);

        Thread.sleep(1000);

        BtyDingdingDayAttendParam btyDingdingDayAttendParam = new BtyDingdingDayAttendParam();
        btyDingdingDayAttendParam.setAttendYearMonth(btyDingdingMonthAttendParam.getAttendYearMonth());
        Map<String,String> map3 = this.btyDingdingDayAttendService.addBtyDingdingDayAttend(btyDingdingDayAttendParam);

        Thread.sleep(1000);

        Map<String,String> map4 = this.btyDingdingMonthAttendService.addBtyDingdingMonthAttend(btyDingdingMonthAttendParam);

        //Map<String,String> map5 = this.btyDingdingMonthAttendService.synchronousliuQin();

        long t2=System.currentTimeMillis();
        long t=(t2-t1)/1000;
        logger.info("******************************************************************************************************");
        logger.info("同步月度考勤信息-用时:{}",t);
        logger.info("******************************************************************************************************");
        XxlJobHelper.log("XXL-JOB, 同步月度考勤信息用时 : " + t);
        XxlJobHelper.log("XXL-JOB, 同步部门 : " + map1.toString());
        XxlJobHelper.log("XXL-JOB, 同步员工 : " + map2.toString());
        XxlJobHelper.log("XXL-JOB, 同步员工每日考勤记录数据 : " + map3.toString());
        XxlJobHelper.log("XXL-JOB, 同步员工每月考勤记录数据 : " + map4.toString());
    }

    /**
     * 同步月度考勤信息到月度考勤表旧表；
     *
     */
    @XxlJob("synchronousliuQin")
    public void synchronousliuQin() throws Exception {
        long t1=System.currentTimeMillis();
        Map<String,String> map5 = this.btyDingdingMonthAttendService.synchronousliuQin();
        long t2=System.currentTimeMillis();
        long t=(t2-t1)/1000;
        logger.info("******************************************************************************************************");
        logger.info("同步月度考勤信息-用时:{}",t);
        logger.info("******************************************************************************************************");
        XxlJobHelper.log("XXL-JOB, 同步月度考勤表旧表用时 : " + t);
        XxlJobHelper.log("XXL-JOB, 同步月度考勤表旧表 : " + map5.toString());
    }
}
